﻿using LuShanTea.DTO;
using LuShanTea.IService;
using LuShanTea.Service.Entities;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LuShanTea.Service
{
    public class PackService : IPackService
    {
        /// <summary>
        /// 添加商品包装类型
        /// </summary>
        /// <param name="name">类型名</param>
        /// <returns>包装类型Id</returns>
        public long AddNew(string name, float price)
        {
            using (LuShanTeaContext context = new LuShanTeaContext())
            {
                BaseService<Pack> baseService = new BaseService<Pack>(context);

                bool isExists = baseService.GetAll().Any(p => p.Name == name);

                if (isExists)
                {
                    throw new ArgumentException("包装名称：" + name + "已存在");
                }

                Pack pack = new Pack()
                {
                    Name = name,
                    Price = price
                };

                context.Packs.Add(pack);
                context.SaveChanges();

                return pack.Id;
            }
        }

        /// <summary>
        /// 获取所有包装类型
        /// </summary>
        /// <returns></returns>
        public PackDTO[] GetAll()
        {
            using (LuShanTeaContext context = new LuShanTeaContext())
            {
                BaseService<Pack> baseService = new BaseService<Pack>(context);

                var packs = baseService.GetAll().AsNoTracking();

                return packs.ToList().Select(p => ToDTO(p)).ToArray();
            }
        }

        /// <summary>
        /// 根据包装Id获取包装
        /// </summary>
        /// <param name="packId"></param>
        /// <returns></returns>
        public PackDTO GetById(long packId)
        {
            using (LuShanTeaContext context = new LuShanTeaContext())
            {
                BaseService<Pack> baseService = new BaseService<Pack>(context);

                var pack = baseService.GetById(packId);

                return pack == null ? null : ToDTO(pack);

            }
        }

        /// <summary>
        /// 根据包装Name获取包装
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public PackDTO GetByName(string name)
        {
            using (LuShanTeaContext context = new LuShanTeaContext())
            {
                BaseService<Pack> baseService = new BaseService<Pack>(context);

                var pack = baseService.GetAll().SingleOrDefault(p => p.Name == name);

                return pack == null ? null : ToDTO(pack);

            }
        }

        /// <summary>
        /// 根据商品Id获取商品包装类型
        /// </summary>
        /// <param name="goodId">商品Id</param>
        /// <returns></returns>
        public PackDTO[] GetPackByGoodId(long goodId)
        {
            using (LuShanTeaContext context = new LuShanTeaContext())
            {
                BaseService<Good> baseService = new BaseService<Good>(context);

                var good = baseService.GetById(goodId);

                if (good == null)
                {
                    throw new ArgumentException(string.Format($"商品Id为：{goodId}不存在"));
                }

                return good.Packs.ToList().Select(p => ToDTO(p)).ToArray();
            }
        }



        /// <summary>
        /// 根据ID软删除数据
        /// </summary>
        /// <param name="packId">包装Id</param>
        public void MarkDeleted(long packId)
        {
            using (LuShanTeaContext context = new LuShanTeaContext())
            {
                BaseService<Pack> baseService = new BaseService<Pack>(context);

                Pack pack = baseService.GetById(packId);

                if (pack == null)
                {
                    throw new ArgumentException(string.Format($"订单Id为：{packId}不存在"));
                }

                baseService.MakeDelete(packId);
            }
        }

        /// <summary>
        /// 更新包装
        /// </summary>
        /// <param name="packId">包装Id</param>
        /// <param name="packName">包装名称</param>
        public void UpdatePack(long packId, string packName, float price)
        {
            using (LuShanTeaContext context = new LuShanTeaContext())
            {
                BaseService<Pack> baseService = new BaseService<Pack>(context);

                Pack pack = baseService.GetById(packId);

                if (pack == null)
                {
                    throw new ArgumentException(string.Format($"订单Id为：{packId}不存在"));
                }

                pack.Name = packName;
                pack.Price = price;

                context.SaveChanges();
            }
        }

        private PackDTO ToDTO(Pack pack)
        {
            PackDTO packDTO = new PackDTO()
            {
                Name = pack.Name,
                CreateDateTime = pack.CreateDateTime,
                Id = pack.Id,
                Price = pack.Price
            };


            return packDTO;
        }
    }
}
